home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / Technical Documentation / develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15 / Application / HFSBackingStore.cp < prev    next >
Encoding:
Text File  |  1996-02-19  |  5.6 KB  |  180 lines  |  [TEXT/CWIE]

  1.  
  2.  
  3. #include "HFSBackingStore.h"
  4. #include "MoreStrings.h"
  5. #include "Exceptions.h"
  6.  
  7. //--------------------------------------------------------------------------------
  8. // THFSBackingStore::THFSBackingStore
  9. //--------------------------------------------------------------------------------
  10. THFSBackingStore::THFSBackingStore()
  11. {
  12.     fRefNum = TOpenFileRefNum();
  13.     fFileEOF = 0;
  14.     // ••• set up an arbitrary FSSpec here in the temporary items folder
  15.     this->OpenFile();
  16. } // THFSBackingStore::THFSBackingStore
  17.  
  18. //--------------------------------------------------------------------------------
  19. // THFSBackingStore::THFSBackingStore
  20. //--------------------------------------------------------------------------------
  21. THFSBackingStore::THFSBackingStore(TFSSpecification& fileSpec)
  22. {
  23.     fRefNum = TOpenFileRefNum();
  24.     fFileEOF = 0;
  25.     fFileSpec = fileSpec;
  26.     
  27.     //
  28.     // Fill in from fileSpec?
  29.     //
  30.     fFileCreator = 0;
  31.     fFileType = 0;
  32.     
  33.     this->OpenFile();
  34. } // THFSBackingStore::THFSBackingStore
  35.  
  36. //--------------------------------------------------------------------------------
  37. // THFSBackingStore::THFSBackingStore
  38. //--------------------------------------------------------------------------------
  39. THFSBackingStore::THFSBackingStore(TFSSpecification& fileSpec, OSType fileCreator, OSType fileType)
  40. {
  41.     fRefNum = TOpenFileRefNum();
  42.     fFileEOF = 0;
  43.     fFileSpec = fileSpec;
  44.     fFileCreator = fileCreator;
  45.     fFileType = fileType;
  46.     
  47.     this->OpenFile();
  48. } // THFSBackingStore::THFSBackingStore
  49.  
  50. //--------------------------------------------------------------------------------
  51. // THFSBackingStore::~THFSBackingStore
  52. //--------------------------------------------------------------------------------
  53. THFSBackingStore::~THFSBackingStore()
  54. {
  55.     this->CloseFile();
  56. } // THFSBackingStore::~THFSBackingStore
  57.  
  58. //--------------------------------------------------------------------------------
  59. // THFSBackingStore::BackingStoreType
  60. //--------------------------------------------------------------------------------
  61. long THFSBackingStore::BackingStoreType() const
  62. {
  63.     return kHFSBackingStore;
  64. }
  65.  
  66. //--------------------------------------------------------------------------------
  67. // THFSBackingStore::CanSaveDocument
  68. //--------------------------------------------------------------------------------
  69. Boolean THFSBackingStore::CanSaveDocument()
  70. {
  71.     //
  72.     // •Should also test to see if the file or volume is locked.
  73.     //
  74.     return TAbstractBackingStore::CanSaveDocument() && (fRefNum.IsOpen());
  75. }
  76.  
  77. //--------------------------------------------------------------------------------
  78. // THFSBackingStore::Read
  79. //--------------------------------------------------------------------------------
  80. void THFSBackingStore::Read(void* bufferStart, Int64 startByteInFile, long numberOfBytes)
  81. {
  82.     //
  83.     // Fail if the file is not open
  84.     //
  85.     if(fRefNum.IsOpen())
  86.     {
  87.         long bytesToRead = numberOfBytes;
  88.         FailErr(fRefNum.SetFilePosition(startByteInFile));
  89.         FailErr(fRefNum.Read(&bytesToRead, bufferStart));
  90.         ASSERT(bytesToRead == numberOfBytes);
  91.     }
  92.     else
  93.         FailErr(fnOpnErr);
  94. } // THFSBackingStore::Read
  95.  
  96. //--------------------------------------------------------------------------------
  97. // THFSBackingStore::Write
  98. //--------------------------------------------------------------------------------
  99. void THFSBackingStore::Write(void* bufferStart, Int64 startByteInFile, long numberOfBytes)
  100. {
  101.     //
  102.     // Fail if the file is not open.
  103.     //
  104.     // CanSaveDocument is (should be) more restrictive than IsOpen
  105.     // because it checks (should check) things like locked volumes
  106.     //
  107.     if(this->CanSaveDocument())
  108.     {
  109.         long bytesToWrite = numberOfBytes;
  110.  
  111.         if(startByteInFile + numberOfBytes > fFileEOF)
  112.         {
  113.             Int64 newEOF = startByteInFile + numberOfBytes;
  114.             FailErr(fRefNum.SetEndOfFile(newEOF));
  115.             fFileEOF = newEOF;
  116.         }
  117.  
  118.         FailErr(fRefNum.SetFilePosition(startByteInFile));
  119.         FailErr(fRefNum.Write(&bytesToWrite, bufferStart));
  120.         ASSERT(bytesToWrite == numberOfBytes);
  121.     }
  122.     //
  123.     // If 'CanSaveDocument' returned false because the volume
  124.     // was locked, then this would give us the wrong error.  Hm.
  125.     //
  126.     else
  127.         FailErr(fnOpnErr);
  128. } // THFSBackingStore::Write
  129.  
  130. //--------------------------------------------------------------------------------
  131. // THFSBackingStore::OpenFile
  132. //--------------------------------------------------------------------------------
  133. void THFSBackingStore::OpenFile()
  134. {
  135.     if(fRefNum.IsOpen() == false)
  136.     {
  137.         //
  138.         // Should create the file if it doesn't exist
  139.         // If the file does exist, then we should require
  140.         // that its file type is what we expect it to be.
  141.         //
  142.         
  143.         //
  144.         // Currently we always open a file read/write.  It
  145.         // might be nice to allow read-only access to a file
  146.         // as well, but currently there's no way to specify that.
  147.         //
  148.         FailErr(fRefNum.Open(fFileSpec));
  149.         
  150.         OffsetInFile theEndOfFile;
  151.         FailErr(fRefNum.GetEndOfFile(&theEndOfFile));
  152.         fFileEOF = theEndOfFile;
  153.     }
  154. } // THFSBackingStore::OpenFile
  155.  
  156. //--------------------------------------------------------------------------------
  157. // THFSBackingStore::CloseFile
  158. //--------------------------------------------------------------------------------
  159. void THFSBackingStore::CloseFile()
  160. {
  161.     //
  162.     // We never want 'Close' to fail.
  163.     //
  164.     fRefNum.Close();
  165. #if 0 //◊
  166.     FlushVol("\p", fFileSpec.VRefNum());    // ◊◊◊ should be a method of fFileSpec, perhaps?
  167. #endif
  168. } // THFSBackingStore::CloseFile
  169.  
  170. //--------------------------------------------------------------------------------
  171. // THFSBackingStore::DocumentName
  172. //--------------------------------------------------------------------------------
  173. void THFSBackingStore::DocumentName(TUpdataDataReference& name)
  174. {
  175.     TString specificationName;
  176.     fFileSpec.Name(specificationName);
  177.     
  178.     name.CopyFrom(TConstDataReference(typeChar, (char*)specificationName.TextStart(), specificationName.StringLength()));
  179. }
  180.